#!/usr/bin/perl

use strict;
use warnings;

# tell Perl where to find MozComics package
BEGIN { push @INC, "/home/mozcomic/scrapers/"; }

use MozComics::JSON;
use MozComics::MySQL;

#use LWP::Simple;
use WWW::Mechanize;
use DateTime;
use DateTime::Format::Strptime;
use HTML::Entities qw(decode_entities);

my $guid = 'aa6482b0-ecf5-11de-8a39-0800200c9a66';
#my $comic = MozComics::JSON->new($guid);
my $comic = MozComics::MySQL->new($guid, 5);
my $comicName = $comic->getName();

my $lastStripRow = $comic->getLastStrip();
my $lastStrip = 0;

my $dateParser = new DateTime::Format::Strptime(
	pattern => '%B %d, %Y',
	locale  => 'en_US'
);

# scrape information from a page
sub scrapePage {
	my $url = shift(@_) or die "$comicName: url not passed to scrapePage";
	my $page = shift(@_) or die "$comicName: page not passed to scrapePage";
	my $data = {};
	$data->{url} = $url;
	my @scraped;

	# scrape 'title' value
	my $titleRegex = qr/\<h1 class="entry-title"\>(.+?)\<\/h1\>/;
	@scraped = $comic->scrapeUnique($page, $titleRegex);
	my $title = $scraped[0];
	$title =~ s/&#8216;/'/g;
	$title =~ s/&#8217;/'/g;
	if($title ne decode_entities($title)) {
		die "$comicName: check decoding of $title";
	}
	$data->{title} = $title;

	if($title eq 'Origami') {
		# http://www.savagechickens.com/2006/11/origami.html
		$data->{extra} = '{"hiddenImage":"http://www.savagechickens.com/images/chickenorigami2.jpg"}';
	}

	# scrape 'strip' value
	my $stripRegex = qr/\<span class="date time published" title="(.+?)"\>(.+?)\<\/span\>/;
	@scraped = $comic->scrapeUnique($page, $stripRegex);
	my $dateString = $scraped[1];
	my $date = $dateParser->parse_datetime($dateString)
	           or die "$comicName: could not parse date ($dateString)";
	my $strip = $date->epoch();

	if($strip <= $lastStrip) {
		$strip = $lastStrip; #die "$comicName: $title is out of order?";
	}
	$strip += 1000;
	$lastStrip = $strip;
	$data->{strip} = $strip;

	# scrape 'image' value
	my $imgRegex = qr/\<div class="entry_content"\>(\s*)\<p\>\<img([^\>]+?)src="(.+?)"/s;
	if(($title eq 'Phantom Mountain') or
		 ($title eq 'Savage Chickens book trailer') or
		 ($title eq 'Savage Chickens appears on The Huffington Post')) {
		# http://www.savagechickens.com/2007/09/phantom-mountain.html
		$data->{image} = '';
	}
	elsif ($title eq 'Savage Chickens returns to FMyLife.com') {
		$data->{image} = 'http://cdn.betacie.net/fmylife/data/en/illust/1e992ecfd1e1d67d1ef6c04ddcdedcb9.jpg';
	}
	else {
		@scraped = $comic->scrapeUnique($page, $imgRegex);
		$data->{image} = $scraped[2];
	}

	return $data;
}


#my $stripPage = get($lastStripRow->{url})
#                or die "$comicName: last page url not found";
my $mech = WWW::Mechanize->new();
$mech->get($lastStripRow->{url});
if(!$mech->success()) {
	die "$comicName: last page url not found";
}
my $stripPage = $mech->content;

my $lastStripData = scrapePage($lastStripRow->{url}, $stripPage);
$comic->assertStripsEqual($lastStripData, $lastStripRow);

$lastStrip = $lastStripRow->{strip};

while($stripPage =~ /\<a  href="([^\>]+?)" rel="next"([^\>]*?)\>Next\<\/a\>/) {
	my $url = $1;

#	$stripPage = get($url) or die "$comicName: next strip page not found";
	$mech->get($url);
	if(!$mech->success) {
		die "$comicName: next strip page not found";
	}
	$stripPage = $mech->content;

	# skip non-comics
	if(not($stripPage =~ /rel="category tag"\>Cartoons\<\/a\>/)) {
		next;
	}
	
	#print "$url\n";
	$comic->addStrip(scrapePage($url, $stripPage));
}

$comic->commit();

